<?php
	
	abstract class AbstractFieldset extends EavAppModel
	{
		/**
		 * Field Model Class name.
		 * @var unknown_type
		 */
		protected	$fieldModelName;
		protected 	$fieldModelAlias;
		var $fieldProperties = array(
			'id'=>array('group'=>"grid"),	
			'name'=>array('group'=>array('add','grid'),'validate'=>array('rule'=>VALID_NOT_EMPTY,'required'=>true))
		);
		function afterInitialize()
		{
			$this->setFieldModelAssociation();
			parent::afterInitialize();
		}
		/**
		 * set assocaition with field model
		 * @param unknown_type $config
		 */
		protected function setFieldModelAssociation($config = array())
		{
			if($this->fieldModelName == null)
			{
				throw new Exception(__("No field model specified"));
			}
			$config['className'] = $this->fieldModelName; 
			$this->bindModel(array('hasMany'=>array($this->getFieldModelAlias()=>$config)),false);
		}
		/**
		 * return instace of field model.
		 */
		function getFieldModel()
		{
			return $this->{$this->getFieldModelAlias()}; 
		}
		/**
		 * Reutrn field model alias
		 */
		function getFieldModelAlias()
		{
			if(!$this->fieldModelAlias)
			{
				$className = $this->fieldModelName;
				$this->fieldModelAlias = array_pop(explode('.',$this->fieldModelName));
			}
			return $this->fieldModelAlias;
		}
		function getFieldsetFieldProperties($id)
		{
			$fields = $this->getFieldModel()->find('all',array('recursive'=>-1,'conditions'=>array($this->foreignKey => $id),'order'=>$this->getFieldModel()->alias.'.order ASC'));
			if($fields)
			{
				$fields = Set::extract("{n}.".$this->getFieldModelAlias(),$fields);
			}
			$fieldProperties = array();
			foreach($fields as $field)
			{
				$name = $field['name'];
				unset($field['name'],$field['id']);
				$fieldProperties[$name] = $field;
			}
			return $fieldProperties;
		}
		
	}
?>